<?php
class statisticsModel extends baseModel{

    //获取html文件
    public function get_html() {

        $starttime=time();
        $starttime=$starttime-6*24*3600;
        $appconfig=appConfig('statistics');
        if(!empty($appconfig['token'])){
        $data=Http::doGet('http://www.clicki.cn/api/trend?token='.$appconfig['token'].'&begindate='.date('Y-m-d',$starttime).'&enddate='.date('Y-m-d').'&sort=1&offset=0&limit=7&viewtb=1',5);
        $data=json_decode($data,true);
        }
        $data=$data['items'];
        if(!empty($data)){
            foreach ($data as $key => $v) {
                $sequence[$key] = $v['date'];
            }
            array_multisort($sequence,SORT_ASC, $data);
            foreach ($data as $key => $value) {
                $jsdata['pageviews'].=$value['pageviews'].',';
                $jsdata['ips'].=$value['ips'].',';
                $jsdata['sessions'].=$value['sessions'].',';
                $jsdata['date'].='"'.$value['date'].'",';
            }
            $jsdata['pageviews']=trim($jsdata['pageviews'],',');
            $jsdata['ips']=trim($jsdata['ips'],',');
            $jsdata['sessions']=trim($jsdata['sessions'],',');
            $jsdata['date']=trim($jsdata['date'],',');
        }

        $html="
        <div class=\"g-home-module s-home-module\">
            <div class=\"u-title u-tt u-tt-md\"> <span class=\"f-fl\">本站7天访问趋势</span> <span class=\"f-fr u-tt-sm \"> <span class=\"u-label u-label-success\">展示量(PV)</span> <span class=\"u-label u-label-warning\">访问次数</span> <span class=\"u-label u-label-info\">独立访客(IP)</span></span>
                <div class=\"f-cb\"></div>
            </div>
            <div class=\"m-chart\">
                <canvas id=\"myChart\" height=\"200\"></canvas>
            </div>
        </div>
        ";
        $html.="
        <script>
        Do.ready('base','chart', function() {
            $(\"#myChart\").attr('width',$(window).width()-50);
            $(\"#myChart\").attr('height','200');
            var ctx = $(\"#myChart\").get(0).getContext(\"2d\");
            var data = {
            labels : [".$jsdata['date']."],
            datasets : [
                {
                    fillColor : \"rgba(14,201,0,0.1)\",
                    strokeColor : \"rgba(14,201,0,1)\",
                    pointColor : \"rgba(14,201,0,1)\",
                    pointStrokeColor : \"#fff\",
                    data : [".$jsdata['pageviews']."]
                },
                {
                    fillColor : \"rgba(151,187,205,0.3)\",
                    strokeColor : \"rgba(151,187,205,1)\",
                    pointColor : \"rgba(151,187,205,1)\",
                    pointStrokeColor : \"#fff\",
                    data : [".$jsdata['sessions']."]
                },
                {
                    fillColor : \"rgba(248,148,6,0.3)\",
                    strokeColor : \"rgba(248,148,6,1)\",
                    pointColor : \"rgba(248,148,6,1)\",
                    pointStrokeColor : \"#fff\",
                    data : [".$jsdata['ips']."]
                }
            ]
        }
            new Chart(ctx).Line(data);
            
        });
        </script>
        ";

        return $html;

    }

}